package com.itshizhan.viewpager2demo


import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var viewPager: ViewPager2
    private lateinit var pageChangeCallback:ViewPager2.OnPageChangeCallback

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewPager = findViewById(R.id.pager)
        val pagerAdapter = SlideAdapter(this)
        // 设置adapter
        viewPager.adapter = pagerAdapter
        // 设置垂直方向
        viewPager.orientation = ViewPager2.ORIENTATION_VERTICAL
        viewPager.layoutDirection = ViewPager2.LAYOUT_DIRECTION_RTL

        // 监听滑动
        pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                Toast.makeText(this@MainActivity, "Page: ${position+1}",
                    Toast.LENGTH_SHORT).show()
            }
        }
        viewPager.registerOnPageChangeCallback(pageChangeCallback)


        // 关联tabLayout
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            tab.text = pagerAdapter.getPageTitle(position)
        }.attach()
    }

}